﻿using Infrastructure.DbUtility;
using Infrastructure.Utility;
using System;
using System.Data;
using System.IO;
using System.Web.UI.WebControls;

public partial class grade : BasePage
{
    const int GRADE_PAGE_SIZE = 50;
    protected string SortCol
    {
        get { return ViewState["SortCol"] == null ? string.Empty : (string)ViewState["SortCol"]; }
        set { ViewState["SortCol"] = value; }
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        this.Context.Items["showNav"] = false;

        if (!IsPostBack) this.LoadGrade(1, string.Empty);

    }

    private void LoadGrade(int index, string keyword)
    {
        string sql = string.Empty;

        string filter = string.IsNullOrWhiteSpace(keyword) ? string.Empty : "[name] LIKE  '%' + {0} + '%'";

        sql = "SELECT COUNT(*) count FROM Grade (NOLOCK) " + (filter == string.Empty ? string.Empty : " WHERE " + filter) + Environment.NewLine;

        if (index == 1) sql += "SELECT TOP " + GRADE_PAGE_SIZE + " * FROM Grade (NOLOCK)" + (filter == string.Empty ? string.Empty : " WHERE " + filter) + SortCol;
        else sql += "SELECT TOP " + GRADE_PAGE_SIZE + " * FROM Grade (NOLOCK) WHERE nid NOT IN (SELECT TOP " + ((index - 1) * GRADE_PAGE_SIZE) + " nid FROM Grade (NOLOCK)" + SortCol + ")" + (filter == string.Empty ? string.Empty : " AND " + filter) + SortCol;

        DbHelper db = new DbHelper();
        var ds = db.ExecuteQueryEx(sql, keyword);

        this.Index = index;
        int count = ds.Tables[0].Rows[0].Value<int>("count");
        this.TotalSize = count / GRADE_PAGE_SIZE + (count % GRADE_PAGE_SIZE == 0 ? 0 : 1);

        listGrade.DataSource = ds.Tables[1].AsEnumerable().Select(r => new { img = GetDefaultGradeImage(r.Value("image")), gid = r.Value<int>("nid"), title = r.Value("name") });
        listGrade.DataBind();
    }
    protected void btnPost_Click(object sender, EventArgs e)
    {
        var keyword = (Request["keyword"] ?? string.Empty).Trim();
        this.Index = 1;
        this.LoadGrade(1, keyword);
    }
    protected void btnNavigate_Click(object sender, EventArgs e)
    {
        string sindex = Request["txtGoTo"];
        int index = 0;
        if (!int.TryParse(sindex, out index) || index < 1) return;

        var keyword = (Request["keyword"] ?? string.Empty).Trim();
        this.Index = index;

        this.LoadGrade(index, keyword);
    }


    protected string GetDefaultGradeImage(string img)
    {
        if (string.IsNullOrWhiteSpace(img) || !File.Exists(img)) return "/content/images/course-108-128.png";
        else return GetSitePath(img);
    }
    protected void btnSort_Click(object sender, EventArgs e)
    {
        var btn = sender as Button;
        if (btn.CommandName == "DATE")
        {
            if (SortCol.StartsWith(" ORDER BY systime"))
            {
                if (!SortCol.EndsWith("ASC")) SortCol = " ORDER BY systime ASC";
                else SortCol = " ORDER BY systime DESC";
            }
            else
            {
                SortCol = " ORDER BY systime ASC";
            }
            btnSortTop.Text = "更新" + (SortCol.EndsWith("ASC") ? "↑" : (SortCol.EndsWith("DESC") ? "↓" : string.Empty));
        }
        var grade = (Request["keyword"] ?? string.Empty).Trim();
        this.LoadGrade(this.Index, grade);
    }



}